package com.bigknow.appstore.proxy.exceptions.processor.impl;

import com.bigknow.appstore.proxy.enumerate.ErrorType;
import com.bigknow.appstore.proxy.enumerate.SecurityType;
import com.bigknow.appstore.proxy.exceptions.ProxyError;
import com.bigknow.appstore.proxy.vo.RequestVo;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;
import java.util.Date;

/**
 *
 * Created by chaos on 2016/9/22.
 */
public final class ErrorHelper {

    public static void throwError(Channel channel, RequestVo requestVo,
                                  ErrorType errorType, String message, String requestContent, String reponseContent) {
        if (requestVo == null) {
            requestVo = new RequestVo();
            requestVo.setCallIp(((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress());
            requestVo.setCallTime(new Date());
            requestVo.setCallUri(null);
        }
        throw new ProxyError(message, errorType, requestVo, requestContent, reponseContent);
    }

    public static void throwResponseEncodingError(Channel channel,RequestVo requestVo){
        throwError(channel, requestVo, ErrorType.RESPONSE_ENCODING_ERROR
                , "proxy response encoding error", null, null);
    }

    public static void throwCallToManyErrorConnectError(Channel channel, RequestVo requestVo, int disConnectTimeout) {
        throwError(channel, requestVo, ErrorType.CALL_TO_MANY_ERROR_CONNECT, "to many error, you cannot call ["
                + disConnectTimeout + "]milliseconds", null, null);
    }


    public static void throwProxyServerNoLongerServeError(Channel channel, RequestVo requestVo) {
        throwError(channel, requestVo, ErrorType.PORXY_NO_LONGER_SERVE, "Proxy server no longer serve ,because is pased", null, null);
    }

    public static void throwNotFullHttpMessageError(Channel channel, RequestVo requestVo) {
        throwError(channel, requestVo, ErrorType.HTTP_DATA_ERROR, "message is error: is not http message", null, null);
    }

    public static void throwNotFullRequestError(Channel channel, RequestVo requestVo) {
        throwError(channel, requestVo, ErrorType.REQUEST_DATA_ERROR, "request is error: is not a full Request message", null, null);
    }

    public static void throwNotFullReponseError(Channel channel, RequestVo requestVo) {
        throwError(channel, requestVo, ErrorType.RESPONSE_DATA_ERROR, "reponse is error: is not a full Response message", null, null);
    }

    //source error
    public static void throwSourceCannotConnError(Channel channel, RequestVo requestVo, String message) {
        throwError(channel, requestVo, ErrorType.SOURCE_CANNOT_CONN, message, null, null);
    }

    //source error
    public static void throwSourceError(Channel channel, RequestVo requestVo, String message) {
        throwError(channel, requestVo, ErrorType.SOURCE_ERROR, message, null, null);
    }

    /**
     *
     * @param channel
     */
    public static void throwUriError(Channel channel,RequestVo requestVo) {
        throwError(channel, requestVo, ErrorType.URI_NO_PASS, "url is not a regist server:", null, null);
    }

    public static void throwTCError(Channel channel, RequestVo requestVo, SecurityType securityType, String judgeKey, long little, long current){
        if(SecurityType.REC_CALL_TIMES_SEC.equals(securityType)) {
            throwError(channel, requestVo, ErrorType.CALL_TIMES_EXCEED
                    , String.format("call times exceed,  Can only call %d times a %s but call for %d times", little, judgeKey, current)
                    , null, null);
        }else{
            throwError(channel, requestVo, ErrorType.CALL_DATAS_EXCEED
                    , String.format("call datas exceed,  Can only call %d bytes a %s but call for %d bytes", little, judgeKey, current)
                    , null, null);
        }
    }

}
